/*
 *                    COPYRIGHT NOTICE
 *
 *           This code is copyright protected by Freedom Inc.
 *
 *              2001-2012 All Rights Are Reserved
 */
package com.freedom.oldsong.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.freedom.oldsong.util.PrivilegeUtil;

/**
 * The privilege tag for group privilege checker.
 * Check if the user has the privilege :
 *   1) skip the included body text if no.
 *   2) evaluate the included body text if yes. 
 *   
 *  Note: The group might be project group id or other kinds of privileges defined 
 *  in Hydra. 
 *
 * @author steven.wang
 *
 */
public class PrivilegeTag extends BodyTagSupport {
	
	/**
	 * @name serialVersionUID
	 * @type long
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * @name log
	 * @type Log
	 */
	private static final Log log = LogFactory.getLog(PrivilegeTag.class);
	
	/**
	 * @name requiredPrivilege
	 * @type String
	 */
	private String requiredPrivilege;
	
	/**
	 * @name getRequiredPrivilege
	 *
	 * @return    
	 * String     
	 * @throws 
	*/
	public String getRequiredPrivilege() {
		return requiredPrivilege;
	}

	/**
	 * @name setRequiredPrivilege
	 *
	 * @param requiredPrivilege    
	 * void     
	 * @throws 
	*/
	public void setRequiredPrivilege(String requiredPrivilege) {
		this.requiredPrivilege = requiredPrivilege;
	}

	/**
	 * @name group
	 * @type String
	 */
	private String group = null;
	
	
	/**
	 * Creates a new instance of PrivilegeTag.
	 *
	 */
	public PrivilegeTag() {
        super();
    }

    /**
     * (non-Javadoc)
     * @see javax.servlet.jsp.tagext.BodyTagSupport#doEndTag()
     */
    public int doEndTag() throws JspException {
        return EVAL_PAGE;
    }

    /**
     * (non-Javadoc)
     * @see javax.servlet.jsp.tagext.BodyTagSupport#doStartTag()
     */
    public int doStartTag() {        
        try {
           if(group != null && !"".equals(group.trim())) {
        	   if(PrivilegeUtil.hasAnyGroupRoleAndPrivilege(group, getRequiredPrivilege())){
        		   return EVAL_BODY_INCLUDE;
        	   } else {
        		   return SKIP_BODY;
        	   }        		   
           } else {
        	   if(PrivilegeUtil.hasAnyPrivilege(getRequiredPrivilege().split(PrivilegeUtil.SEPARATOR))){
        		   return EVAL_BODY_INCLUDE;
        	   } 
           }        	
        } catch (Exception e) {
            log.error("Error occurred when checking group privilege.", e);
        }       

        return SKIP_BODY;
    }
    
    /**
     * Get group value
     * 
     * @return group
     */
    /**
     * @name getGroup
     *
     * @return    
     * String     
     * @throws 
    */
    public String getGroup() {
		return group;
	}

	/**
	 * Set group value
	 * 
	 * @param group
	 */
	/**
	 * @name setGroup
	 *
	 * @param group    
	 * void     
	 * @throws 
	*/
	public void setGroup(String group) {
		this.group = group;
	}	

}
